GBML manual / Save

Saving a game

Saving copies named values from the console's working memory into the cartridge's battery-backed save memory, which is the only place anything survives being switched off.

What survives being switched off

Everything your game holds while it runs - every variable, every instance, the room you are in - lives in the console's RAM. RAM is fast working memory, and it forgets everything the moment the power goes off. Nothing you have made in GBML so far survives that.

A GBA cartridge can also carry a second, small kind of memory called SRAM, kept alive by a tiny battery inside the cartridge. Anything written there is still there next time. That is what a save is: a copy of some values, written into SRAM, and read back into RAM the next time the game starts. An emulator does the same job with a .sav file next to the game.

So saving is never automatic. You decide which values matter, you write them, and you read them back.

Keys and values

GBML's save store works like a labeled shelf. Each item on it has a key, a name you choose, written as text in quotes, and a value stored under that name.

// Step event of an options screen
save_set("high_score", 1200);
save_set("has_key", true);
save_set("volume", 0.5);

A value can be a whole number (int), a number with a fractional part (real), or true/false (bool). Text and lists cannot be saved. If you need to save a list, store each item under its own key, such as "slot_0", "slot_1".

The five functions

FunctionWhat it does
save_set(key, value)Puts a value on the shelf under a key.
save_get(key, default)Reads a value back. If the key was never set, you get default.
save_has(key)True if the key has ever been set and not cleared.
save_clear()Empties the shelf.
save_commit()Writes the shelf to the cartridge.

The default in save_get is what makes a first play-through work. A brand new game has no saved values, so save_get("high_score", 0) gives 0 instead of failing.

The default also sets the kind you are asking for. If you saved a bool under a key and then ask for a real with save_get, you get your default back, not the bool's raw bits reinterpreted as a number. A key that holds one kind of value does not turn into another kind by accident.

Set is not save

This is the part that catches most people. save_set writes to a working copy of the shelf that lives in RAM. It does not touch the cartridge. Only save_commit copies the whole working copy into SRAM. Switch the console off before a commit and every save_set since the last one is lost.

save_clear works the same way. It empties the working copy, and the cartridge only changes at the next save_commit.

This split is on purpose. Writing to SRAM is slow, and you may want to update a value many times a second, such as counting kills. So the usual pattern is to call save_set as often as you like, and call save_commit at a real save point: a save menu, a checkpoint, a room change.

When the game starts, the engine reads the cartridge into the working copy for you, before any of your code runs. Your part is to read the values you want, usually once in a Create event:

// Create event of the player
var best = save_get("high_score", 0);

What the store can hold

The whole cartridge has 32 KB of SRAM, and it is a hard limit of the hardware. GBML uses a fixed, small part of it.

Keys you build while the game runs count toward the 80 too. A key made with a template string, such as $"enemy_{n}_dead", is one key for every different value of n.

Project Settings has a Save size field. It is only checked against the 32 KB hardware ceiling, and it does not change the limits above. The number of keys has a box of its own, Names the game can save; the length of a key is built into the engine.

Some features spend keys of their own. A cutscene that is set to play once remembers that it has played with one key, named starting with gbcut_. Avoid that prefix in your own keys. See Cutscenes.

Collision layers do not spend keys. The game remembers which layer each room is on in a small place of its own, so your keys can never crowd it out.

One game's save is not another's

The store is stamped with an identifier made from your project's own id or name, and with a number for how the project counts positions. When the game starts and the stamp on the cartridge does not match, whether because it is blank, damaged, or was written by a different project, the engine throws it away and starts with an empty shelf. It never tries to guess what old bytes might have meant. The next save_commit then writes a fresh save over it.

The practical consequence is that a project keeps its saves between builds as long as it keeps its id and name, and starts empty if either changes. Moving an older project over to the top-left rule also changes the stamp, so saves written before the move are thrown away.

Saving the player's choices

Some things are not game progress but the player's own settings, and they get lost the same way. action_rebind changes a button's meaning until the game is switched off. To keep a player's control choices, save the button when they pick it, and call action_rebind again at startup from what you saved. The same idea works for a sound volume: sound_gain sets it for this run only, so save the number and set it again on start.

Example

// Collision event of a checkpoint object, when the player touches it
save_set("checkpoint_room", 2);
save_set("coins", other.coins);
save_commit();

Two values are put on the shelf, then the whole shelf is written to the cartridge. Until save_commit runs, the two save_set lines exist only in RAM.

See also: save_setsave_commitsave_getaction_rebindMoving between roomsCutscenes